﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Reflection.Emit;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using System.Net.NetworkInformation;

namespace 玩具商城管理系统.znew
{
    public partial class NorderT : Form
    {
        public NorderT()
        {
            InitializeComponent();
        }
        SqlDataAdapter dahot,daorder;
        DataSet ds = new DataSet();

        public static string path_source = "";
        public static string path_source2 = "";
        public static string path_source3 = "";
        void init()//初始化
        {
            DB.GetCn();
            string str = "select * from hot where hot_clientname='" + login.username + "'";
            dahot = new SqlDataAdapter(str, DB.cn);
            dahot.Fill(ds, "hot_info");
            string sdr = "select * from orderT where orderT_cid='" + login.username + "'";
            daorder = new SqlDataAdapter(sdr, DB.cn);
            daorder.Fill(ds, "orderT_info");
        }
        void showac()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvorder.Columns.Add(acCode);
            int columnIndex = dgvorder.Columns["acCode"].Index;
            dgvorder.Columns[columnIndex].Width = 80;
            acCode.DisplayIndex = 0;
        }
        void showAll()
        {

            DB.GetCn();
            string str = "select * from hot where hot_clientname='" + login.username + "'";
            SqlCommand cmd = new SqlCommand(str, DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                int index = dgvorder.Rows.Add();
                dgvorder.Rows[index].Cells[0].Value = rdr[4];
                dgvorder.Rows[index].Cells[1].Value = rdr[1];
                dgvorder.Rows[index].Cells[2].Value = rdr[2];
                dgvorder.Rows[index].Cells[3].Value = rdr[5];
                dgvorder.Rows[index].Cells[4].Value = rdr[6];
                dgvorder.Rows[index].Cells[5].Value = rdr[8];
                try
                {
                    Image imageColumn = Image.FromFile(Application.StartupPath + rdr[9]);
                    dgvorder.Rows[index].Cells["C"].Value = imageColumn;

                }
                catch
                {
                    Image imgeColumn = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                    dgvorder.Rows[index].Cells["C"].Value = imgeColumn;
                }
                try
                {
                    Image imageColumn2 = Image.FromFile(Application.StartupPath + rdr[10]);
                    dgvorder.Rows[index].Cells["Column7"].Value = imageColumn2;
                }
                catch
                {
                    Image imgeColumn2 = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                    dgvorder.Rows[index].Cells["Column7"].Value = imgeColumn2;

                }
                try
                {
                    Image imageColumn3 = Image.FromFile(Application.StartupPath + rdr[11]);
                    dgvorder.Rows[index].Cells["Column8"].Value = imageColumn3;
                }
                catch
                {
                    Image imgeColumn3 = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                    dgvorder.Rows[index].Cells["Column8"].Value = imgeColumn3;
                }
            }

            rdr.Close();
        }

        private void NorderT_Load(object sender, EventArgs e)
        {
            if (radioButton5.Checked)
            {
                label6.Visible = true;
                label6.Text = "⭐⭐⭐⭐⭐";
                label6.ForeColor = Color.Goldenrod;
            }
            dgvorder.RowTemplate.Height = 70;
            showac();
            init();
            showAll();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)//查询订单
        {
            if (txtfind.Text == "")
            {
                MessageBox.Show("请输入查询关键字");
            }
            else
            {
                dgvorder.Rows.Clear();
                DB.GetCn();
                string str = "select * from hot where hot_commodityname like '%" + txtfind.Text + "%'";
                SqlCommand cmd = new SqlCommand(str, DB.cn);
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    int index = dgvorder.Rows.Add();
                    dgvorder.Rows[index].Cells[0].Value = rdr[4];
                    dgvorder.Rows[index].Cells[1].Value = rdr[1];
                    dgvorder.Rows[index].Cells[2].Value = rdr[2];
                    dgvorder.Rows[index].Cells[3].Value = rdr[5];
                    dgvorder.Rows[index].Cells[4].Value = rdr[6];
                    dgvorder.Rows[index].Cells[5].Value = rdr[8];
                    try
                    {
                        Image imageColumn = Image.FromFile(Application.StartupPath + rdr[9]);
                        dgvorder.Rows[index].Cells["C"].Value = imageColumn;
                    }
                    catch
                    {
                        Image imgeColumn = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                        dgvorder.Rows[index].Cells["C"].Value = imgeColumn;
                    }
                }
                rdr.Close();
                DB.cn.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvorder.Rows.Count; i++)
            {
                if (dgvorder.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    if (dgvorder.Rows[i].Cells["Column4"].Value.ToString() == "已完成")
                    {

                        DB.GetCn();
                        string filename;
                        string fileFolder;
                        string filename2;
                        string fileFolder2;
                        string filename3;
                        string fileFolder3;
                        string dateTime = "";
                        filename = Path.GetFileName(path_source);
                        filename2 = Path.GetFileName(path_source2);
                        filename3 = Path.GetFileName(path_source3);
                        dateTime += DateTime.Now.Year.ToString();
                        dateTime += DateTime.Now.Month.ToString();
                        dateTime += DateTime.Now.Day.ToString();
                        dateTime += DateTime.Now.Hour.ToString();
                        dateTime += DateTime.Now.Minute.ToString();
                        dateTime += DateTime.Now.Second.ToString();
                        filename = dateTime + filename;
                        filename2 = dateTime + filename2;
                        filename3 = dateTime + filename3;
                        fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\" + "评论"
                            + "\\";
                        fileFolder += filename;
                        fileFolder2 = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\" + "评论"
                           + "\\";
                        fileFolder2 += filename2;
                        fileFolder3 = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\" + "评论"
                           + "\\";
                        fileFolder3 += filename3;

                        DataRow[] ProRows = ds.Tables["hot_info"].Select("hot_clientname= '" + login.username + "'");
                        if (pictureBox1.Image != null)
                        {
                            File.Copy(path_source, fileFolder, true);

                            ProRows[i]["hot_phto"] = "\\Prod_Images\\" + "评论" + "\\"
                           + filename;
                        }
                        else
                        {
                            ProRows[i]["hot_phto"] = DBNull.Value;
                        }
                        if (pictureBox2.Image != null)
                        {
                            File.Copy(path_source2, fileFolder2, true);

                            ProRows[i]["hot_phto2"] = "\\Prod_Images\\" + "评论" + "\\"
                           + filename2;
                        }
                        else
                        {
                            ProRows[i]["hot_phto2"] = DBNull.Value;
                        }
                        if (pictureBox3.Image != null)
                        {
                            File.Copy(path_source3, fileFolder3, true);

                            ProRows[i]["hot_phto3"] = "\\Prod_Images\\" + "评论" + "\\"
                           + filename3;
                        }
                        else
                        {
                            ProRows[i]["hot_phto3"] = DBNull.Value;
                        }
                        ProRows[i]["hot_language"] = txtlanguage.Text;
                        if (radioButton6.Checked)
                        {

                            ProRows[i]["hot_show"] = "yes";
                        }
                        else
                        {

                            ProRows[i]["hot_show"] = "no";
                        }
                        ProRows[i]["hot_star"] = label6.Text;
                        try
                        {
                            SqlCommandBuilder dbProduct = new SqlCommandBuilder(dahot);
                            dahot.Update(ds, "hot_info");
                            MessageBox.Show("评价成功");
                        }
                        catch (SqlException ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("要完成订单后才可以评价亲");
                    }


                }
            }
        }

        private void dgvorder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvorder.Rows.Count > 0)
            {
                for (int i = 0; i < dgvorder.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dgvorder.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;

                    }
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dgvorder.Rows.Clear();
            showAll();

        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (path_source2 != "")
            {

                OpenFileDialog ofd = new OpenFileDialog();
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    path_source3 = ofd.FileName;
                    pictureBox3.Image = Image.FromFile(path_source3);
                    pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
                    pictureBox4.Visible = true;
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path_source = ofd.FileName;
                pictureBox1.Image = Image.FromFile(path_source);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox2.Visible = true;
            }

        }
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (path_source != "")
            {

                OpenFileDialog ofd = new OpenFileDialog();
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    path_source2 = ofd.FileName;
                    pictureBox2.Image = Image.FromFile(path_source2);
                    pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                    pictureBox3.Visible = true;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = false;
            dgvorder.Visible = true;
        }


        private void button6_Click(object sender, EventArgs e)
        {
            int selectedRowIndex = -1;
            foreach (DataGridViewRow row in dgvorder.Rows)
            {
                DataGridViewCheckBoxCell selectedCell = row.Cells["acCode"] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(selectedCell.Value))
                {
                    selectedRowIndex = row.Index;
                    break;
                }
            }

            if (selectedRowIndex >= 0)
            {
                string imglj = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg").ToString();
                string imgPath = dgvorder.Rows[selectedRowIndex].Cells[6].Value.ToString();
                if (dgvorder.Rows[selectedRowIndex].Cells[5].Value.ToString() != "" || imglj != imgPath)
                {
                    MessageBox.Show("您已经评论过了，不能修改评价了哦");
                }
                else if (dgvorder.Rows[selectedRowIndex].Cells[4].Value.ToString() == "已退货")
                {
                    MessageBox.Show("退货的商品不能评价");
                }
                else
                {

                  
                    txtlanguage.Text = null;
                    pictureBox1.Image = null;
                    pictureBox2.Image = null;
                    pictureBox3.Image = null;
                    pictureBox2.Visible = false;
                    pictureBox3.Visible = false;
                    pictureBox4.Visible = false;
                    dgvorder.Visible = false;
                    groupBox2.Visible = true;
                }
            }
            else
            {
                MessageBox.Show("请选择你需要评价的商品");
            }


        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                label6.Visible = true;
                label6.Text = "⭐";
                label6.ForeColor = Color.Goldenrod;
            }

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                label6.Visible = true;
                label6.Text = "⭐⭐";
                label6.ForeColor = Color.Goldenrod;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                label6.Visible = true;
                label6.Text = "⭐⭐⭐";
                label6.ForeColor = Color.Goldenrod;
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                label6.Visible = true;
                label6.Text = "⭐⭐⭐⭐";
                label6.ForeColor = Color.Goldenrod;
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked)
            {
                label6.Visible = true;
                label6.Text = "⭐⭐⭐⭐⭐";
                label6.ForeColor = Color.Goldenrod;
            }

        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            Nwuliu wuliu =new Nwuliu();
            wuliu.ShowDialog();
        }

        private void button7_Click(object sender, EventArgs e)
        {
           pj pj = new pj();
            pj.ShowDialog();

        }

    }
}